package com.peanut.youfupay.controller;


import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.response.ResponseMessage;
import com.peanut.youfupay.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 优付服务商退款异步通知
 * 1、通知逻辑：累计5次，商户返回SUCCESS则停止
 * 每隔5秒通知1次，共5次
 *
 * @author mf
 */
@Controller
@RequestMapping("/refund")
@Slf4j
public class RefundNotifyController {
    @Autowired
    BaseService baseService;

    @PostMapping(value = "/notify", produces = "text/xml;charset=UTF-8")
    @ResponseBody
    public String doBusiness(@RequestBody String encryptedMsg) {
        log.info("优付服务商退款异步通知返回报文{}", encryptedMsg);
        ResponseMessage encryptedResponseMessage = JSON.parseObject(encryptedMsg, ResponseMessage.class);
        ResponseMessage responseMessage = baseService.handleResponse(encryptedResponseMessage);
        String notifyData = responseMessage.getResData();
        log.info("优付服务商退款异步通知返回结果-notifyData{}", notifyData);
        //返回给优付的结果
        return "SUCCESS";
    }

    /**优付付款异步通知返回encryptedMsg完整报文示例*/

    /*
   {
           "funCode":"6025",
           "merId":"1642993239923136",
           "resCode":"0000",
           "resData":"Vo7eCgC6hnqwLZZYe1gUELb/gVHDSFl6HCDlcuh7CRHlm9RbAtWJ6U+mm/lsF9o9obstGTGOp1Oue7gYSXIuhh9pQUedoAIeCtVD6qaxcQqjKX5bnYbp6nNf95X75Yq6ThI5AVKjRo9CCIkUwaN0avj8EcALeYaYCWnLx+aYj8uUQ2eTaJ3cvGI7Cyoo1yhOaDcvCUfhmrLcjVzZGLnWblnI0+obG21snLrMroawPktuTDJEYlngeeJXlPHD0HNcoyr2nMfoZIajXnA9TZ0k1mr+d7SwZDwj",
           "resMsg":"成功",
           "sign":"Ukn/1oitKNzP1OW+ioIQ25Qz4sLwEFA72+eTLQymtoMhBZFC/W0Nr5zQo6wPujoC2UtgshKhWK656GzwU/TPRPwjpkylg7kcV1p8Ev3eYhCkXmUY3KXsjm/UngY3A7kcylKnI/1Pdr1SJaNozy/8/wS5mY/ms7XgR/7nSvzkcKI=",
           "version":"V1.0",
           "levyId":"30481"

   }
    */
    /**优付付款异步通知返回ResData明文示例*/
    /*
    {
            "amount":1,
            "levyId":"30481",
            "merId":"1642993239923136",
            "merNo":"11111",
            "merbath":"000000",
            "orderEndTime":"2023-04-19 14:28:21",
            "platFee":2,
            "refundFinishTime":"2023-04-19 14:28:21",
            "refundOrderId":"333333333333"
    }
    */

}
